<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <title>rocketmq集群模式 | JAVA</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="MQ">
  
  
  
  
  <meta name="description" content="Master-Slave主从模式 生产者发数据只会发到Master上，然后slave 从Master pull数据同步下来，跟master保存一份一模一样的数据。 消费者在系统获取消息时候会先发送请求到Master Broker上去,请求获取一批消息，此时Master Broker是会返回一批消息给消费者系统的。然后Master Broker在返回消息给消费者系统的时候， 会根据当时Master">
<meta name="keywords" content="MQ">
<meta property="og:type" content="article">
<meta property="og:title" content="RocketMQ集群模式">
<meta property="og:url" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ集群模式/index.html">
<meta property="og:site_name" content="JAVA">
<meta property="og:description" content="Master-Slave主从模式 生产者发数据只会发到Master上，然后slave 从Master pull数据同步下来，跟master保存一份一模一样的数据。 消费者在系统获取消息时候会先发送请求到Master Broker上去,请求获取一批消息，此时Master Broker是会返回一批消息给消费者系统的。然后Master Broker在返回消息给消费者系统的时候， 会根据当时Master">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203081710737.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203082435078.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203082959966.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207084050039.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207084942295.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085123126.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085144645.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085319420.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085522551.png">
<meta property="og:image" content="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085809732.png">
<meta property="og:updated_time" content="2021-01-26T02:56:20.085Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RocketMQ集群模式">
<meta name="twitter:description" content="Master-Slave主从模式 生产者发数据只会发到Master上，然后slave 从Master pull数据同步下来，跟master保存一份一模一样的数据。 消费者在系统获取消息时候会先发送请求到Master Broker上去,请求获取一批消息，此时Master Broker是会返回一批消息给消费者系统的。然后Master Broker在返回消息给消费者系统的时候， 会根据当时Master">
<meta name="twitter:image" content="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203081710737.png">
  
    <link rel="alternate" href="/atom.xml" title="JAVA" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("/css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("/css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("/css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css">

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css">
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css">
  

</head>
</html>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="border-width: 0;">
                <p>JAVA</p>
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder>
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词...">
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </ul></div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-RocketMQ集群模式" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost">
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" itemprop="name">
      RocketMQ集群模式
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2020/12/04/RocketMQ集群模式/" class="article-date">
	  <time datetime="2020-12-04T10:52:53.000Z" itemprop="datePublished">2020-12-04</time>
	</a>

      
    <a class="article-category-link" href="/categories/消息队列/">消息队列</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Master-Slave主从模式"><a href="#Master-Slave主从模式" class="headerlink" title="Master-Slave主从模式"></a>Master-Slave主从模式</h1><blockquote>
<p>生产者发数据只会发到Master上，然后slave 从Master pull数据同步下来，<br>跟master保存一份一模一样的数据。</p>
<p>消费者在系统获取消息时候会先发送请求到Master Broker上去,请求获取一批消息，<br>此时Master Broker是会返回一批消息给消费者系统的。然后Master Broker在返回消息给消费者系统的时候，</p>
<p>会根据当时Master Broker的负载情况和Slave Broker的同步情况，向消费者系统建议下一次拉取消息的时候是从Master Broker拉取还是从Slave Broker拉取。</p>
</blockquote>
<blockquote>
<p>在<strong>RocketMQ 4.5版本</strong>之前，都是用Slave Broker同步数据，尽量保证数据不丢失，但是一旦Master故障了，Slave是没法自动切换成Master的，在这种情况下，如果Master Broker宕机了，这时就得手动做一些运维操作，把Slave Broker重新修改一些配置，重启机器给调整为Master Broker，这是有点麻烦的，而且会导致中间一段时间不可用。Master-Slave模式不是彻底的高可用模式，他没法实现自动把Slave切换为Master</p>
</blockquote>
<p><strong>master-slave主从模式消息复制</strong></p>
<ul>
<li><strong>同步复制</strong></li>
</ul>
<p>同步复制方式是等Master和Slave均写成功后才反馈给客户端写成功状态，在同步复制方式下，如果Master出故障,Slave上有全部的备份数据，容易恢复，但是同步复制会增大数据写入，延迟，降低系统吞吐量。</p>
<ul>
<li><strong>异步复制</strong></li>
</ul>
<p>异步复制方式是只要Master写成功，即可反馈给客户端写成功状态。在异步复制方式下，系统有较低低延迟和较高吞吐量，但是如果Master出故障，有些故障未及时写入slave，可能造成数据丢失。</p>
<h1 id="基于DLedger高可用自动切换"><a href="#基于DLedger高可用自动切换" class="headerlink" title="基于DLedger高可用自动切换"></a>基于DLedger高可用自动切换</h1><h2 id="什么是DLedger"><a href="#什么是DLedger" class="headerlink" title="什么是DLedger"></a>什么是DLedger</h2><blockquote>
<p>DLedger有一个CommitLog机制，你把数据交给他，他会写入CommitLog磁盘文件里去，这些他做的第一件事。如下图可见，基于DLedger技术来实现Broker高可用框架，实际就是用DLedger先替换原来Broker自己来管理的CommitLog，由DLedger来管理CommitLog</p>
</blockquote>
<p><img src="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203081710737.png" alt="image-20201203081710737"></p>
<h2 id="DLedger基于Raft协议选举Leader-Broker"><a href="#DLedger基于Raft协议选举Leader-Broker" class="headerlink" title="DLedger基于Raft协议选举Leader Broker"></a>DLedger基于Raft协议选举Leader Broker</h2><blockquote>
<p>简单来说,三台Broker机器启动时，他们都会投票自己作为Leader,然后把这个投票发送给其它Broker。</p>
<p>举个例子,Broker01是投票给自己的，Broker02是投票给自己的，Broker03是投票给自己的，他们都把自己的投票发送给了别<br>人。</p>
<hr>
<p>第一轮选举中，Broker01会收到别人的投票，他发现自己是投票给自己，但是Broker02投票给Broker02自己，Broker03投票给Broker03自己，似乎每个人都很自私，都在投票给自己，所以第一轮选举是失败的</p>
<hr>
<p>每个人会进入一个随机时间的休眠，比如说Broker01休眠3秒，Broker02休眠5秒，Broker03休眠4秒。</p>
<hr>
<p>Broker01必然是先苏醒过来的，他苏醒过来之后，直接会继续尝试投票给自己，并且发送自己的选票给别人。接着Broker03休眠4秒后苏醒过来，他发现Broker01已经发送来了一个选票是投给Broker01自己的，此时他自己因为没投票，所以会尊重别人的选择，就直接把票投给Broker01了，同时把自己的投票发送给别人。接着Broker02苏醒了，他收到了Broker01投票给Broker01自己，收到了Broker03也投票给了Broker01，那么他此时自己是没投票<br>的，直接就会尊重别人的选择，直接就投票给Broker01，并且把自己的投票发送给别人</p>
<hr>
<p>此时所有人都会收到三张投票，都是投给Broker01的，那么Broker01就会当选为Leader。</p>
</blockquote>
<p><strong>Raft协议中选举leader算法的简单描述，简单来说，他确保有人可以成为Leader的核心机制就是一轮选举不出来Leader的话，</strong><br><strong>就让大家随机休眠一下，先苏醒过来的人会投票给自己，其他人苏醒过后发现自己收到选票了，就会直接投票给那个人。</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203082435078.png" alt="image-20201203082435078"></p>
<h2 id="DLedger基于Raft协议进行副本同步"><a href="#DLedger基于Raft协议进行副本同步" class="headerlink" title="DLedger基于Raft协议进行副本同步"></a>DLedger基于Raft协议进行副本同步</h2><p><strong>数据同步会分为两个阶段，一个是uncommitted阶段，一个是commited阶段</strong></p>
<blockquote>
<p>首先Leader Broker上的DLedger收到一个条数据之后，会标记为uncommitted状态，然后通过自己的DLedgerServer组件把这个</p>
<p>uncommitted数据发送给Follower Broker的DLedgerServer。</p>
</blockquote>
<p><img src="https://jameslin23.gitee.io/2020/12/01/RocketMQ/image-20201203082959966.png" alt="image-20201203082959966"></p>
<blockquote>
<p>接着Follower Broker的DLedgerServer收到uncommitted消息之后，必须返回一个ack给Leader Broker的DLedgerServer,然后如果Leader Broker收到超过半数的Follower Broker返回的ack之后，就会将消息标记为committed状态。</p>
<p>然后Leader Broker上的DLedgerServer就会发送commited消息给Follower Broker机器的DLedgerServer,让他们也把消息标记commited状态</p>
</blockquote>
<p><strong>这个就是基于Raft协议实现的两阶段完成的数据同步机制。</strong></p>
<h2 id="如何使用心跳维护leader地位"><a href="#如何使用心跳维护leader地位" class="headerlink" title="如何使用心跳维护leader地位"></a>如何使用心跳维护leader地位</h2><p><strong>当选节点之后，首选要维护自己的leader地位，他要告诉集群其它节点，我是集群中leader，你们要成为我的follower，负责同步我的数据，并且保证只要我还活着，你们就不要妄想重新进行选举</strong></p>
<ol>
<li>每隔几秒钟leader节点会向所有follower节点发送心跳请求；</li>
<li>follower收到心跳请求之后，更新本地倒计时时间，同时给leader节点一个确认回复</li>
<li>leader节点收到过半数follower节点回复，则说明自己还是leader</li>
<li>如果没有收到过半数follower节点回复，则会变更为candidate状态，重新触发选举；同样的，如果follower节点一直没有收到leader节点的心跳请求,follower节点也会变更为candidate状态，触发leader选举。</li>
</ol>
<h1 id="实践部署"><a href="#实践部署" class="headerlink" title="实践部署"></a>实践部署</h1><h2 id="下载MQ，要求版本4-5以上"><a href="#下载MQ，要求版本4-5以上" class="headerlink" title="下载MQ，要求版本4.5以上"></a>下载MQ，要求版本4.5以上</h2><ol>
<li><a href="https://github.com/apache/rocketmq/releases" target="_blank" rel="noopener">https://github.com/apache/rocketmq/releases</a> 这里下载需要的RocketMQ版本</li>
<li>unzip rocketmq-all-4.7.0-bin-release.zip</li>
</ol>
<h2 id="修改配置文件"><a href="#修改配置文件" class="headerlink" title="修改配置文件"></a>修改配置文件</h2><p>服务器说明：(生产中应该将 NameServer 部署到其他服务器中，在这为了方便，与Broker部署在一起)</p>
<table>
<thead>
<tr>
<th align="center"><strong>服务器</strong></th>
<th align="center"><strong>ip</strong></th>
<th align="center"><strong>安装的服务</strong></th>
</tr>
</thead>
<tbody><tr>
<td align="center">服务器1-主</td>
<td align="center">192.168.10.8</td>
<td align="center">DLedger，Broker，NameServer</td>
</tr>
<tr>
<td align="center">服务器2-从</td>
<td align="center">192.168.10.10</td>
<td align="center">DLedger，Broker，NameServer</td>
</tr>
<tr>
<td align="center">服务器3-从</td>
<td align="center">192.168.10.3</td>
<td align="center">DLedger，Broker，NameServer</td>
</tr>
</tbody></table>
<p><strong>服务器1配置-Master</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#  ## 集群名</span><br><span class="line">brokerClusterName = RaftCluster</span><br><span class="line">## broker组名，同一个RaftClusterGroup内，brokerName名要一样</span><br><span class="line">brokerName=RaftNode00</span><br><span class="line">## 监听的端口</span><br><span class="line">listenPort=30911</span><br><span class="line"># 你设置的NameServer地址和端口</span><br><span class="line">namesrvAddr=192.168.10.8:9876;192.168.10.10:9876;192.168.10.3:9876</span><br><span class="line"># 数据存储路径</span><br><span class="line">storePathRootDir=/tmp/rmqstore/node1</span><br><span class="line">storePathCommitLog=/tmp/rmqstore/node1/commitlog</span><br><span class="line"># 是否启动 DLedger</span><br><span class="line">enableDLegerCommitLog=true</span><br><span class="line"># DLedger Raft Group的名字，建议和 brokerName 保持一致</span><br><span class="line">dLegerGroup=RaftNode00</span><br><span class="line"># DLedger Group 内各节点的端口信息，同一个 Group 内的各个节点配置必须要保证一致</span><br><span class="line">dLegerPeers=n1-192.168.10.8:40911;n2-192.168.10.10:40912;n3-192.168.10.3:40913</span><br><span class="line">#节点 id, 必须属于 dLegerPeers 中的一个；同 Group 内各个节点要唯一</span><br><span class="line">dLegerSelfId=n1</span><br><span class="line"># 发送线程个数，建议配置成 Cpu 核数</span><br><span class="line">sendMessageThreadPoolNums=16</span><br></pre></td></tr></table></figure>

<p><strong>服务器2配置-Slave</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">brokerClusterName = RaftCluster</span><br><span class="line">brokerName=RaftNode00</span><br><span class="line">listenPort=30922</span><br><span class="line">namesrvAddr=192.168.10.8:9876;192.168.10.3:9876;192.168.10.10:9876</span><br><span class="line">storePathRootDir=/tmp/rmqstore/node2</span><br><span class="line">storePathCommitLog=/tmp/rmqstore/node2/commitlog</span><br><span class="line">enableDLegerCommitLog=true</span><br><span class="line">dLegerGroup=RaftNode00</span><br><span class="line">dLegerPeers=n1-192.168.10.8:40911;n2-192.168.10.10:40912;n3-192.168.10.3:40913</span><br><span class="line">## must be unique</span><br><span class="line">dLegerSelfId=n2</span><br><span class="line">sendMessageThreadPoolNums=16</span><br></pre></td></tr></table></figure>

<p><strong>服务器3配置-Slave</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">brokerClusterName = RaftCluster</span><br><span class="line">brokerName=RaftNode00</span><br><span class="line">listenPort=30933</span><br><span class="line">namesrvAddr=192.168.10.8:9876;192.168.10.3:9876;192.168.10.10:9876</span><br><span class="line">storePathRootDir=/tmp/rmqstore/node03</span><br><span class="line">storePathCommitLog=/tmp/rmqstore/node03/commitlog</span><br><span class="line">enableDLegerCommitLog=true</span><br><span class="line">dLegerGroup=RaftNode00</span><br><span class="line">dLegerPeers=n1-192.168.10.8:40911;n2-192.168.10.10:40912;n3-192.168.10.3:40913</span><br><span class="line">## must be unique</span><br><span class="line">dLegerSelfId=n3</span><br><span class="line">sendMessageThreadPoolNums=16</span><br></pre></td></tr></table></figure>

<h2 id="启动集群"><a href="#启动集群" class="headerlink" title="启动集群"></a>启动集群</h2><ol>
<li><p><strong>在服务器1 执行</strong></p>
<blockquote>
<p>先启动mqnamesrv服务 nohup sh mqnamesrv &gt;/dev/null 2&gt;log &amp;</p>
<p>再启动broker服务 nohup sh mqbroker -c ../conf/dledger/broker-node1.conf &gt;/dev/null 2&gt;log &amp;</p>
</blockquote>
</li>
<li><p><strong>在服务器2 执行</strong></p>
<blockquote>
<p>先启动mqnamesrv服务 nohup sh mqnamesrv &gt;/dev/null 2&gt;log &amp;</p>
<p>再启动broker服务 nohup sh mqbroker -c ../conf/dledger/broker-node2.conf &gt;/dev/null 2&gt;log &amp;</p>
</blockquote>
</li>
<li><p><strong>在服务器3 执行</strong></p>
<blockquote>
<p>先启动mqnamesrv服务 nohup sh mqnamesrv &gt;/dev/null 2&gt;log &amp;</p>
<p>再启动broker服务 nohup sh mqbroker -c ../conf/dledger/broker-node3.conf &gt;/dev/null 2&gt;log &amp;</p>
</blockquote>
<p><strong>内存不足，修改runbroker.sh，runserver.sh 参数</strong></p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">JAVA_OPT=<span class="string">"<span class="variable">$&#123;JAVA_OPT&#125;</span> -server -Xms8g -Xmx8g -Xmn4g"</span></span><br><span class="line">JAVA_OPT=<span class="string">"<span class="variable">$&#123;JAVA_OPT&#125;</span> -XX:MaxDirectMemorySize=1g</span></span><br></pre></td></tr></table></figure>

<p><strong>连接超时，查看防火墙状态</strong></p>
<p>查看防火墙服务状态 <code>systemctl status firewalld</code></p>
<p>将防火墙关闭 <code>systemctl stop firewalld</code></p>
<p><strong>查看集群状态</strong></p>
<p><strong>sh mqadmin clusterList -n 127.0.0.1:9876</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207084050039.png" alt="image-20201207084050039"></p>
<p><strong>使用控制台查看</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207084942295.png" alt="image-20201207084942295"></p>
<p><strong>测试生产者-消费者</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085123126.png" alt="image-20201207085123126"></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085144645.png" alt="image-20201207085144645"></p>
<p><strong>测试集群高可用，关闭192.168.10.8节点，集群重新竞选master</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085319420.png" alt="image-20201207085319420"></p>
<p><strong>发送消息</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085522551.png" alt="image-20201207085522551"></p>
<p><strong>重启192.168.10.8节点,变成slave</strong></p>
<p><img src="https://jameslin23.gitee.io/2020/12/04/RocketMQ%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F/image-20201207085809732.png" alt="image-20201207085809732"></p>
<h1 id="配置参数说明"><a href="#配置参数说明" class="headerlink" title="配置参数说明"></a>配置参数说明</h1><table>
<thead>
<tr>
<th>参数名</th>
<th>默认值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>listenPort</td>
<td>10911</td>
<td>接受客户端连接的监听端口</td>
</tr>
<tr>
<td>namesrvAddr</td>
<td>null</td>
<td>nameServer 地址</td>
</tr>
<tr>
<td>brokerIP1</td>
<td>网卡的 InetAddress</td>
<td>当前 broker 监听的 IP</td>
</tr>
<tr>
<td>brokerIP2</td>
<td>跟 brokerIP1 一样</td>
<td>存在主从 broker 时，如果在 broker 主节点上配置了 brokerIP2 属性，broker 从节点会连接主节点配置的 brokerIP2 进行同步</td>
</tr>
<tr>
<td>brokerName</td>
<td>null</td>
<td>broker 的名称</td>
</tr>
<tr>
<td>brokerClusterName</td>
<td>DefaultCluster</td>
<td>本 broker 所属的 Cluser 名称</td>
</tr>
<tr>
<td>brokerId</td>
<td>0</td>
<td>broker id, 0 表示 master, 其他的正整数表示 slave</td>
</tr>
<tr>
<td>storePathCommitLog</td>
<td>$HOME/store/commitlog/</td>
<td>存储 commit log 的路径</td>
</tr>
<tr>
<td>storePathConsumerQueue</td>
<td>$HOME/store/consumequeue/</td>
<td>存储 consume queue 的路径</td>
</tr>
<tr>
<td>mappedFileSizeCommitLog</td>
<td>1024 * 1024 * 1024(1G)</td>
<td>commit log 的映射文件大小</td>
</tr>
<tr>
<td>deleteWhen</td>
<td>04</td>
<td>在每天的什么时间删除已经超过文件保留时间的 commit log</td>
</tr>
<tr>
<td>fileReservedTime</td>
<td>72</td>
<td>以小时计算的文件保留时间</td>
</tr>
<tr>
<td>brokerRole</td>
<td>ASYNC_MASTER</td>
<td>SYNC_MASTER/ASYNC_MASTER/SLAVE</td>
</tr>
<tr>
<td>flushDiskType</td>
<td>ASYNC_FLUSH</td>
<td>SYNC_FLUSH/ASYNC_FLUSH SYNC_FLUSH 模式下的 broker 保证在收到确认生产者之前将消息刷盘。ASYNC_FLUSH 模式下的 broker 则利用刷盘一组消息的模式，可以取得更好的性能。</td>
</tr>
<tr>
<td>enableDLegerCommitLog</td>
<td>是否启动 DLedger</td>
<td>true</td>
</tr>
<tr>
<td>dLegerGroup</td>
<td>DLedger Raft Group的名字，建议和 brokerName 保持一致</td>
<td>RaftNode00</td>
</tr>
<tr>
<td>dLegerPeers</td>
<td>DLedger Group 内各节点的端口信息，同一个 Group 内的各个节点配置必须要保证一致</td>
<td>n0-127.0.0.1:40911;n1-127.0.0.1:40912;n2-127.0.0.1:40913</td>
</tr>
<tr>
<td>dLegerSelfId</td>
<td>节点 id, 必须属于 dLegerPeers 中的一个；同 Group 内各个节点要唯一</td>
<td>n0</td>
</tr>
<tr>
<td>sendMessageThreadPoolNums</td>
<td>发送线程个数，建议配置成 Cpu 核数</td>
<td>16</td>
</tr>
</tbody></table>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://raw.githubusercontent.com/iTimeTraveler/iTimeTraveler.github.io/site/source/about/donate/images/WeChanQR.png',
  alipayImage: 'https://raw.githubusercontent.com/iTimeTraveler/iTimeTraveler.github.io/site/source/about/donate/images/AliPayQR.jpg'
});
</script>
      
      
      <div>
        <ul class="post-copyright">
          <li class="post-copyright-author">
          <strong>本文作者:  </strong>LeBron Tao
          </li>
          <li class="post-copyright-link">
          <strong>本文链接:  </strong>
          <a href="/2020/12/04/RocketMQ集群模式/" target="_blank" title="RocketMQ集群模式">https://jameslin23.gitee.io/2020/12/04/RocketMQ集群模式/</a>
          </li>
          <li class="post-copyright-license">
            <strong>版权声明:   </strong>
            本博客所有文章除特别声明外，均采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
            许可协议。转载请注明出处
          </li>
         
        </ul>
<div>
</div></div>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/MQ/">MQ</a></li></ul>

      

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/04/RocketMQ拉取策略/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          RocketMQ拉取策略
        
      </div>
    </a>
  
  
    <a href="/2020/12/04/RocketMQ单机模式/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">RocketMQ单机模式</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Master-Slave主从模式"><span class="nav-number">1.</span> <span class="nav-text">Master-Slave主从模式</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#基于DLedger高可用自动切换"><span class="nav-number">2.</span> <span class="nav-text">基于DLedger高可用自动切换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是DLedger"><span class="nav-number">2.1.</span> <span class="nav-text">什么是DLedger</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DLedger基于Raft协议选举Leader-Broker"><span class="nav-number">2.2.</span> <span class="nav-text">DLedger基于Raft协议选举Leader Broker</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DLedger基于Raft协议进行副本同步"><span class="nav-number">2.3.</span> <span class="nav-text">DLedger基于Raft协议进行副本同步</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何使用心跳维护leader地位"><span class="nav-number">2.4.</span> <span class="nav-text">如何使用心跳维护leader地位</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实践部署"><span class="nav-number">3.</span> <span class="nav-text">实践部署</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#下载MQ，要求版本4-5以上"><span class="nav-number">3.1.</span> <span class="nav-text">下载MQ，要求版本4.5以上</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改配置文件"><span class="nav-number">3.2.</span> <span class="nav-text">修改配置文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动集群"><span class="nav-number">3.3.</span> <span class="nav-text">启动集群</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#配置参数说明"><span class="nav-number">4.</span> <span class="nav-text">配置参数说明</span></a></li></ol>
    
    </div>
  </aside>

</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2013 - 2021 JAVA All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            JAVA
          </div>
          <div class="panel-body">
            Copyright © 2021 LeBron Tao All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>